<?php
class ProjectApp extends CommonApp {
   
   	//学生项目栏目界面
    public function studentAct(){
        $userid = My::arr2var($_SESSION,'member.memberid');
        $search['where'] = "userid={$userid}";
        $keyword = I('get.keyword');
        if($keyword!='')
            $search['where'].=" AND team_name LIKE '%{$keyword}%'";
        $this->assign('keyword',$keyword);
        $search['page'] = My::arr2var($_REQUEST,'page').(isset($_SESSION['pagesize'])?$_SESSION['pagesize']:'');//用于参与缓存标识
        $search['time'] = isset($_COOKIE['time']) ? $_COOKIE['time'] : '';
        $result=My::getCache($search,array('pre'=>'project'));
        if($result == ''){
            $total = DB('project')->where($search['where'])->cache()->count();
            $result['page'] = My::page($total,array('pagesize'=>1,'buttonNum'=>5));
            //数据
            $result['data'] = DB('project')->where($search['where'])->limit($result['page']['limits'])->order('adddate DESC,id DESC')->cache()->select();
            unset($result['id']);
            My::setCache($search,$result,array('pre'=>'project'));
        }
        $this->assign('result',$result);
		$this->display();
    }

    //老师项目栏目界面
    public function teacherAct(){
        $userid = My::arr2var($_SESSION,'member.memberid');
        $search['where'] = "teacher_id={$userid}";
        $keyword = I('get.keyword');
        if($keyword!='')
            $search['where'].=" AND team_name LIKE '%{$keyword}%'";
        $this->assign('keyword',$keyword);
        $search['page'] = My::arr2var($_REQUEST,'page').(isset($_SESSION['pagesize'])?$_SESSION['pagesize']:'');//用于参与缓存标识
        $search['time'] = isset($_COOKIE['time_t']) ? $_COOKIE['time_t'] : '';
        $result=My::getCache($search,array('pre'=>'project'));
        if($result == ''){
            $total = DB('project')->where($search['where'])->cache()->count();
            $result['page'] = My::page($total,array('pagesize'=>1,'buttonNum'=>5));
            //数据
            $result['data'] = DB('project')->where($search['where'])->limit($result['page']['limits'])->order('adddate DESC,id DESC')->cache()->select();
            unset($result['id']);
            My::setCache($search,$result,array('pre'=>'project'));
        }
        $this->assign('result',$result);
        $this->display();
    }

    //添加编辑项目
    public function modifyAct(){
        //获取项目id
        $id = I('get.id',0);
        $result = array();
        if(My::is_id($id)){
            //当前id文章数据
            $result = DB('project')->clear()->where("id={$id}")->find();
            $result['params'] = My::decodeJson($result['params']);
        }
        $result['teacher'] = DB('homeuser')->clear()->where("type=1 AND power_1=2 AND power_2=2")->select();
        foreach ($result['teacher'] as &$value) {
            $value['params'] = My::decodeJson($value['params']);
        }
        $this->assign('result',$result);
        $this->display();
    }

    //项目验证
    public function infoAct(){
    	$params = My::arr2var($_POST,'params');
        $userid = My::arr2var($_SESSION,'member.memberid');
    	if(!My::is_string($params['item']))
            $this->ajaxReturn(300,'项目名称填写有误！');
        if(!My::is_string($params['team']))
            $this->ajaxReturn(300,'团队名称填写有误！');
        if(isset($_POST['params']['company'])){
            foreach($_POST['params']['company']['name_c'] as $key => $value) {
                if(!My::is_string($value,array('c'))){
                    $key = $key+1;
                    $this->ajaxReturn(300,'第'."$key".'个队员的姓名填写有误！');
                }
            }
            foreach($_POST['params']['company']['sex_c'] as $key => $value) {
                 if(!in_array($value, array('男','女'))){
                    $key = $key+1;
                    $this->ajaxReturn(300,'第'."$key".'个队员的性别填写有误！');
                }
            }
            foreach($_POST['params']['company']['phone_c'] as $key => $value) {
                if(!My::is_tel($value,array('c'))){
                    $key = $key+1;
                    $this->ajaxReturn(300,'第'."$key".'个队员的手机号码填写有误！');
                }
            }
            foreach($_POST['params']['company']['xy_n'] as $key => $value) {
                if(!My::is_string($value,array('c'))){
                    $key = $key+1;
                    $this->ajaxReturn(300,'第'."$key".'个队员的学院填写有误！');
                }
            }
            foreach($_POST['params']['company']['grade_n'] as $key => $value) {
                if(empty($value)){
                    $key = $key+1;
                    $this->ajaxReturn(300,'第'."$key".'个队员的班级填写有误！');
                }
            }
            foreach($_POST['params']['company'] as $key => $value){
                foreach($value as $keys => $values){
                   $_POST['params']['company'][$keys][$key] = $values;
                   unset($_POST['params']['company'][$key]);
                }
            }
        }
       	if(!My::is_string($params['team_mean']))
            $this->ajaxReturn(300,'项目意义填写有误！');
        if(!My::is_string($params['team_content']))
            $this->ajaxReturn(300,'项目内容填写有误！');
        if(isset($_POST['params']['teacher']))
        	$_POST['teacher_name'] = $_POST['params']['teacher']['name_t'];
        $id = $_POST['id'] = My::arr2var($_POST,'id');
        $_POST['team_name'] = $params['item'];
        $_POST['student_name'] = DB('homeuser')->clear()->where("id={$userid}")->getField('username');
        unset($params['team_name']);
        if(My::is_id($id)){
            $_POST['editdate'] = date("Y-m-d H:i:s");
            $ok = DB('project')->clear()->save($_POST);
            if($ok>=0)  $this->ajaxReturn(200,$params['team']."项目修改成功");
            else        $this->ajaxReturn(300,$params['team']."项目修改失败");
        }else{
            $_POST['adddate'] = date("Y-m-d H:i:s");
            $_POST['power'] = '0';//添加时处于未审核状态
            $_POST['userid'] = $userid;
            $ok = DB('project')->clear()->add($_POST);
            if($ok>=0)  $this->ajaxReturn(200,$params['team']."项目添加成功");
            else        $this->ajaxReturn(300,$params['team']."项目添加失败");
        }
    }

    //删除项目
    public function deteleAct(){
        $id = My::arr2var($_POST,'id');
        if(My::is_id($id)){
            $ok = DB('project')->where("id={$id}")->delete(); 
            if($ok>=0)   $this->ajaxReturn(200,My::arr2var($_POST,'team_name')."项目删除成功！");
            else        $this->ajaxReturn(300,My::arr2var($_POST,'team_name')."项目删除失败！");
        }else
            $this->ajaxReturn(300,My::arr2var($_POST,'team_name')."项目参数出现错误！");
    } 

    //项目详情
    public function project_infoAct(){
        $id = My::arr2var($_GET,'id');
        $result = DB('project')->clear()->where("id={$id}")->find();
        $result['params'] = My::decodeJson($result['params']);
        $this->assign('result',$result);
        $this->display();
    }

    //同意成为指导老师
    public function checkAct(){
        $check = My::arr2var($_POST,'check');
        $ok = DB('project')->clear()->where("id={$check}")->setField('power','1');
        $this->ajaxReturn(200,'您已同意成为该项目的指导老师');
    }
}